package com.yer.note.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.yer.note.R;
import com.yer.note.activity.KeepDiaryActivity;
import com.yer.note.adapter.DiaryAdapter;
import com.yer.note.entity.Diary;
import com.yer.note.entity.User;
import com.yer.note.service.myItemTouchHelperCallBack;
import com.yer.note.utils.BaseUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class HomeFragment extends Fragment {
    private RefreshLayout refreshLayout;
    private View root;
    private ArrayList<Diary> diaries = new ArrayList<>();
    private RecyclerView recyclerview;
    private DiaryAdapter diaryAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    //创建Fragemnt视图的方法
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (root == null){
            //获得布局文件
            root = inflater.inflate((R.layout.fragment_home),container, false);

        }
        //初始化数据
        initData();
        //初始化RecylerView组件的方法
        initRecyclerview();
        refreshLayout = root.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
                diaryAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(2000);
            }
        });
        return  root;
    }

    private void initData() {
        diaries.clear();
        List<Diary> list = Diary.listAll(Diary.class);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUseruuid().equals(User.user.getMyuuid())){
                diaries.add(list.get(i));
            }
        }
        System.out.println(diaries);
        diaries.sort(new Comparator<Diary>() {
            @Override
            public int compare(Diary d1, Diary d2) {
                return -d1.getTime().compareTo(d2.getTime());
            }
        });
    }
    private void initRecyclerview() {
        //获得Recyclerview
        recyclerview = (RecyclerView)root.findViewById(R.id.recyclerview_home);
        User user = User.findById(User.class,1);
        //创建adapter类的对象
        diaryAdapter = new DiaryAdapter(getActivity(), diaries,user.getName());
        diaryAdapter.setOnItemClickLitener(new DiaryAdapter.OnItemClick() {
            @Override
            public void onItemLongClick(String diaryUUid) {
                String news = "正在跳转到日记编辑页面";
                BaseUtils.shortTipInCoast(getActivity(),news);
                Intent intent = new Intent(getActivity(), KeepDiaryActivity.class);
                System.out.println("onItemLongClick:"+diaryUUid);
                intent.putExtra("diaryUUid",diaryUUid);
                startActivity(intent);
            }
        });
        //将对象作为参数通过setAdapter方法设置给recylerview；
        recyclerview.setAdapter(diaryAdapter);
        //这步骤必须有，这是选择RecylerView的显示方式
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        ItemTouchHelper.Callback callback = new myItemTouchHelperCallBack(diaryAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recyclerview);
    }
}